You are given a 0-indexed string num
of length n
consisting of digits.
Return true
if for every indexi
in the range0 <= i < n
, the digiti
occursnum[i]
times innum
, otherwise returnfalse
.
Input: num = "1210" Output: true Explanation: num[0] = '1'. The digit 0 occurs once in num. num[1] = '2'. The digit 1 occurs twice in num. num[2] = '1'. The digit 2 occurs once in num. num[3] = '0'. The digit 3 occurs zero times in num. The condition holds true for every index in "1210", so return true.
Input: num = "030" Output: false Explanation: num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num. num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num. num[2] = '0'. The digit 2 occurs zero times in num. The indices 0 and 1 both violate the condition, so return false.
n == num.length
1 <= n <= 10
num
consists of digits.
implSolution{pubfndigit_count(num:String) -> bool{letmut count = [0;10];for d in num.bytes(){ count[(d - b'0')asusize] += 1;} num.bytes().enumerate().all(|(i, d)| count[i] == (d - b'0')asusize)}}